<?php
/*
    OpenLib
    Copyright (C) 2011  Michael Gukov, Pham Thi Thuy Duong

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


namespace App\Views\Reader;

use Sun\Sun;
use Sun\Html;
use App\Models;

class Profile extends Main {
    public function __construct() {
        parent::__construct();
        $this->data_scene = 'Main';
    }

    public function ReaderInfo() {
        $users = new Models\User();
        $sess = new Models\Session();
        $pofile = $users->UserProfile($sess->UserId());

        $table = new Html\Table();
        $table->SetAttribute('class', 'QueryTable');
        $table->AddBody();


        $i = 0;
        foreach ($pofile as $key => $val) {
            $row = $table->First()->AddRow()->Added();
            $row->AddData()->Added()->Add(new Html\Text($key));
            $row->AddData()->Added()->Add(new Html\Text($val));

            ++$i;
            if ($i % 2) $row->SetAttribute('class', 'Odd');
        }


        $img = new Html\Img(array(
            'src' => 'pub/media/images/user_info_64.png',
            'alt' => 'sdfd',
            'style' => 'padding: 0.4em; border: none'
        ));

        $div = new Html\Table();
        $div->AddBody();
        $div->SetAttribute('class', 'ProfileTable');

        $row = $div->First()->AddRow()->Added();
        $row->AddData()->Added()->Add($img);
        $row->AddData()->Added()->Add($table);

        return $div;
    }

    public function IssueList() {
        $user = new Models\User();
        $sess = new Models\Session();
        $edit = new Models\Edition();

        $issues = $user->GetIssues($sess->UserId());

        $rows = array();
        for ($i = 0; $i < count($issues); ++$i) {
            $rows[$i] = array($i + 1, $edit->GetItemBrief($issues[$i]['edit_id'], $issues[$i]['item_id']));
            $rows[$i][1]->AddRow('iss_date', new Html\Text('Дата выдачи'),
                    new Html\Text($issues[$i]['iss_date']), true);
            $rows[$i][1]->AddRow('ret_date', new Html\Text('Дата возврата'),
                    new Html\Text($issues[$i]['ret_date']), true);

            $remain = strtotime($issues[$i]['ret_date']) - time();
            $remain = (int)($remain / 86400);
            $veryhot = false;

            if ($remain > 0) {
                $tit = 'Осталось, дней';
            } else {
                $remain = -$remain;
                $tit = 'Просрочена, дни';
                $veryhot = true;
            }
            $rows[$i][1]->AddRow('remain', new Html\Text($tit),
                    new Html\Text($remain), true, $veryhot);
        }

        $table = new Html\QueryTable($rows, array("№", "Описание"));
        $table->id = "OrderTable";
        $table->style = "width: 100%;";

        $rows = $table->First()->Childs();

        for ($i = 1; $i < count($rows); ++$i) {
            $id = $i ? $issues[$i - 1]['order_id'] : "";

            $rows[$i]->First()->SetAttribute('class', 'ControlCell');
        }

        $div = new Html\Div();
        $div->Add($table);

        return $div;
   }
}
